﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using Windows.Storage;

namespace CC98.Data
{
    public class AccountManager
    {
        private static readonly object _syncRoot = new object();

        private static volatile AccountManager _instance;

        public static AccountManager Instance
        {
            get
            {
                if (_instance == null)
                    lock (_syncRoot)
                        if (_instance == null)
                            _instance = new AccountManager();
                return _instance;
            }
        }

        private AccountManager()
        {
            // Init
            this.Load();
        }

        /// <summary>
        /// Accounts
        /// </summary>
        public IEnumerable<Account> Accounts
        {
            get
            {
                return this._accounts;
            }
        }

        /// <summary>
        /// Save accounts
        /// </summary>
        public void Save()
        {
            ApplicationDataContainer container = null;
            if (!ApplicationData.Current.RoamingSettings.Containers.TryGetValue("Account", out container))
            {
                container = ApplicationData.Current.RoamingSettings.CreateContainer("Account", ApplicationDataCreateDisposition.Always);
            }
            foreach(var account in this._accounts)
            {
                ApplicationDataCompositeValue value = new ApplicationDataCompositeValue();
                account.Write(value);
                container.Values[account.Username] = value;
            }
        }

        public event EventHandler Updated;

        /// <summary>
        /// Load accounts
        /// </summary>
        /// <returns></returns>
        protected void Load()
        {
            try
            {
                // Load accounts
                ApplicationDataContainer container = null;
                if (ApplicationData.Current.RoamingSettings.Containers.TryGetValue("Account", out container))
                {
                    foreach (var setting in container.Values)
                    {
                        var compositeValue = (setting.Value as ApplicationDataCompositeValue);
                        if (compositeValue == null)
                        {
                            System.Diagnostics.Debug.WriteLine("[AccountManager]Account setting reading error. Value is not ApplicationDataCompositeValue");
                        }
                        else
                        {
                            this._accounts.Add(new Account(compositeValue));
                        }
                    }
                }
            }
            catch (Exception error)
            {
                System.Diagnostics.Debugger.Break();
            }
        }

        protected virtual void OnUpdated()
        {
            if (this.Updated != null)
                this.Updated(this, new EventArgs());
        }

        private readonly List<Account> _accounts = new List<Account>();
    }
}
